﻿// 平差通用函数

using System;
using System.Collections.Generic;
using System.IO;
using System.Text.RegularExpressions;

namespace AdjPlane
{
    //平差常用函数类
    public static class AdjCmn
    {
        //cot(A) = cos(A)/sin(A);
        public static double Cot(double A)
        {
            return 1 / Math.Tan(A);
        }
        //打印矩阵
        public static void Matprint(double[,] A)
        {
            int m = A.GetLength(0);
            int n = A.GetLength(1);
            for (int i = 0; i < m; i++)
            {
                for (int j = 0; j < n; j++)
                {
                    Print(string.Format("{0,8}",A[i, j].ToString("f3")));
                }
                Console.WriteLine();
            }
        }
        //输出矩阵到文件
        public static void Matfprint(double[,] A, string path)
        {
            int m = A.GetLength(0);
            int n = A.GetLength(1);
            List<string> str = new List<string>();
            for (int i = 0; i < m; i++)
            {
                string row = "";
                for (int j = 0; j < n; j++)
                {
                    row+=string.Format("{0,8}", A[i, j].ToString("f3"));
                }
                str.Add(row);
            }
            File.WriteAllLines(path, str);
        }
        //字符串1中是否包含str2中的字符串
        public static int Strstr(string str1, string[] str2)
        {
            for (int i = 0; i < str2.Length; i++)
                if (str1 == str2[i].ToString())
                    return 1;
            return 0;
        }
        // 判断数字
        public static int IsNumber(string str)
        {
            Regex regex = new Regex(@"^[0-9]+$");
            for (int i = 0; i < str.Length; i++)
                if (!regex.IsMatch(str[i].ToString()) && str[i] != '.')
                    return 0;
            return 1;
        }
        // 是否只有字母
        public static int IsOnlyWord(string str)
        {
            Regex regex = new Regex(@"^[a-zA-Z]+$");
            for (int i = 0; i < str.Length; i++)
                if (!regex.IsMatch(str[i].ToString()))
                    return 0;
            return 1;
        }
        //中英文字符串对齐
        public static string PadLeftEx(string str, int totalByteCount)
        {
            int count = GetHanNumFromString(str);
            string w = str.PadLeft(totalByteCount - count);
            return w;
        }
        //获取字符串中的汉字个数
        public static int GetHanNumFromString(string str)
        {
            int count = 0;
            Regex regex = new Regex(@"^[\u4E00-\u9FA5]{0,}$");//汉字在Unicode中的范围

            for (int i = 0; i < str.Length; i++)
                if (regex.IsMatch(str[i].ToString()))
                    count++;
            return count;
        }
        //打印
        public static void Print<T>(T A)
        {
            Console.WriteLine(A.ToString());
        }
        // 点积
        public static double Dot(double[] a,double[] b, int len)
        {
            double sum = 0;
            for (int i = 0; i < len; i++)
                sum += a[i] * b[i];
            return sum;
        }
        // 平方
        public static double[] Square(double[] arr)
        {
            for (int i = 0; i < arr.Length; i++)
                arr[i] = arr[i] * arr[i];
            return arr;
        }
        // 绝对值
        public static double[] Abs(double[] arr)
        {
            for (int i = 0; i < arr.Length; i++)
                arr[i] = Math.Abs(arr[i]);
            return arr;
        }
        // 数组按列存储
        public static double[] ColArray(double[,] M)
        {
            int i, j, m, n;
            m = M.GetLength(0);
            n = M.GetLength(1);
            double[] ret = new double[m * n];
            for (i = 0; i < m; i++)
                for (j = 0; j < n; j++)
                    ret[i + j * m] = M[i, j];
            return ret;
        }
        //求中位数
        public static double Median(double[] arr)
        {
            Array.Sort(arr);
            int n = arr.Length;
            double med;
            if (n % 2 == 0)
                med = arr[n / 2];
            else
                med = 0.5 * (arr[(n + 1) / 2] + arr[(n - 1) / 2]);
            return med;
        }
        //绝对中位差
        public static double Mad(double[] arr)
        {
            double med = Median(arr);
            int n = arr.Length;
            double[] arr2 = new double[n];
            for (int i = 0; i < n; i++)
                arr2[i] = Math.Abs(arr[i] - med);
            return Median(arr2);
        }
        //求均值
        public static double Mean(double[] A)
        {
            int len = A.Length;
            double sum = 0;
            for (int i = 0; i < len; i++)
                sum += A[i];
            return sum / len;
        }
        //求最大值及其对应的序号
        public static double[] Max(double[] A)
        {
            double[] ret = new double[2];
            int idx = 0;
            double max = A[0];
            for (int i = 1; i < A.Length; i++)
            {
                if (max < A[i])
                {
                    max = A[i];
                    idx = i;
                }
            }
            ret[0] = max;
            ret[1] = idx;
            return ret;
        }
        public static double[] Min(double[] A)
        {
            double[] ret = new double[2];
            int idx = 0;
            double min = A[0];
            for (int i = 1; i < A.Length; i++)
            {
                if (min > A[i])
                {
                    min = A[i];
                    idx = i;
                }
            }
            ret[0] = min;
            ret[1] = idx;
            return ret;
        }
        //数组转链表
        public static List<T> Nums2List<T>(T[] N)
        {
            List<T> ret = new List<T>();
            for (int i = 0; i < N.Length; i++)
                ret.Add(N[i]);
            return ret;
        }
    }
}
